package guda.house.web.action.fee;

import guda.grape.autogen.common.page.BizResult;
import guda.grape.util.RequestUtil;
import guda.house.biz.ElecRuleBiz;
import guda.house.biz.beans.vo.JsonResult;
import guda.house.biz.enums.SysVarTypeEnum;
import guda.house.biz.form.rule.ElecRuleEditForm;
import guda.house.biz.form.rule.ElecRuleForm;
import guda.house.biz.query.ElecRuleQuery;
import guda.house.biz.query.RoleQuery;
import guda.house.biz.query.SysVarQuery;
import guda.house.biz.util.ErrorCode;
import guda.house.dal.dao.SysVarDAO;
import guda.house.dal.dataobject.ElecRuleDO;
import guda.house.dal.dataobject.SysVarDO;
import guda.house.web.helper.ResponseHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * Created by well on 15/11/11.
 */
@Controller
public class ElecRuleAction {

    @Autowired
    private ElecRuleBiz elecRuleBiz;
    @Autowired
    private SysVarDAO sysVarDAO;

    @RequestMapping(value = "fee/elec/list.htm", method = RequestMethod.GET)
    public String list(HttpServletRequest request, ModelMap modelMap) {
        int pageId = RequestUtil.getInt(request, "pageNo");
        int pageSize = RequestUtil.getInt(request, "pageSize");
        ElecRuleQuery elecRuleQuery = new ElecRuleQuery();
        elecRuleQuery.setPageNo(pageId);
        elecRuleQuery.setPageSize(pageSize);
        modelMap.put("query",elecRuleQuery);
        BizResult bizResult = elecRuleBiz.list(elecRuleQuery);
        if (bizResult.success) {
            modelMap.putAll(bizResult.data);
            return "fee/elec/list.vm";
        } else {
            return "redirect:/error.htm?code="+bizResult.code;
        }
    }

    @RequestMapping(value = "fee/elec/edit.htm", method = RequestMethod.GET)
    public String edit(HttpServletRequest request, ModelMap modelMap, ElecRuleEditForm elecRuleEditForm,
                       BindingResult result, Map<String,Object> model) {
        long id = RequestUtil.getLong(request, "id");
        BizResult bizResult = elecRuleBiz.queryForEdit(id);
        if (bizResult.success) {
            modelMap.putAll(bizResult.data);
            elecRuleEditForm.initForm(((ElecRuleDO) bizResult.data.get("elecRuleDO")));
            return "fee/elec/edit.vm";
        } else {
            return "redirect:/error.htm?code="+bizResult.code;
        }

    }

    @RequestMapping(value = "fee/elec/detail.htm", method = RequestMethod.GET)
    public String detail(HttpServletRequest request, ModelMap modelMap) {
        long id = RequestUtil.getLong(request, "id");
        BizResult bizResult = elecRuleBiz.detail(id);
        if (bizResult.success) {
            modelMap.putAll(bizResult.data);
            return "fee/elec/detail.vm";
        } else {
            return "redirect:/error.htm?code="+bizResult.code;
        }

    }

    @RequestMapping(value = "fee/elec/create.htm", method = RequestMethod.GET)
    public String create(HttpServletRequest request, ModelMap modelMap, ElecRuleForm elecRuleForm,
                         BindingResult result) {
        return "fee/elec/create.vm";
    }

    @RequestMapping(value = "fee/elec/doCreate.htm", method = RequestMethod.POST)
    public String doCreate(HttpServletRequest request, ModelMap modelMap,@Valid ElecRuleForm elecRuleForm,
                           BindingResult result) {
        if (result.hasErrors()) {
            if(elecRuleForm.getVillageId()!=null){
                SysVarQuery sysVarQuery = new SysVarQuery();
                sysVarQuery.setVillageId(elecRuleForm.getVillageId());
                sysVarQuery.setVarType(SysVarTypeEnum.building.name());
                sysVarQuery.setPageSize(1000);
                List<SysVarDO> sysVarDOs = sysVarDAO.selectForView(sysVarQuery);
                modelMap.put("buildingTypeList",sysVarDOs);
            }
            return "fee/elec/create.vm";
        }
        if(elecRuleForm.getMinValue() == null){

            if(elecRuleForm.getMaxValue() == null){
                result.rejectValue("maxValue","notNull","不能为空");
                return "fee/elec/create.vm";
            }
            elecRuleForm.setMinValue(0);
        }
        if(elecRuleForm.getMaxValue()!= null && elecRuleForm.getMinValue()!=null && elecRuleForm.getMinValue() !=0){
            if(elecRuleForm.getMaxValue() < elecRuleForm.getMinValue()){
                int water = elecRuleForm.getMaxValue();
                elecRuleForm.setMaxValue(elecRuleForm.getMinValue());
                elecRuleForm.setMinValue(water);
            }
        }
        ElecRuleDO elecRuleDO = elecRuleForm.toDO();
        BizResult bizResult = elecRuleBiz.create(elecRuleDO);
        if (bizResult.success) {
            return "redirect:/fee/elec/list.htm";
        } else {
            return "redirect:/error.htm?code="+bizResult.code;
        }

    }

    @RequestMapping(value = "fee/elec/doUpdate.htm", method = RequestMethod.POST)
    public String doUpdate(HttpServletRequest request, HttpServletResponse response,ModelMap modelMap,@Valid ElecRuleEditForm elecRuleEditForm,
                           BindingResult result, Map<String,Object> model) {
        if (result.hasErrors()) {
            if(elecRuleEditForm.getVillageId()!=null){
                SysVarQuery sysVarQuery = new SysVarQuery();
                sysVarQuery.setVillageId(elecRuleEditForm.getVillageId());
                sysVarQuery.setVarType(SysVarTypeEnum.building.name());
                sysVarQuery.setPageSize(1000);
                List<SysVarDO> sysVarDOs = sysVarDAO.selectForView(sysVarQuery);
                modelMap.put("buildingTypeList",sysVarDOs);
            }
            return "fee/elec/edit.vm";
        }
        ElecRuleDO elecRuleDO = elecRuleEditForm.toDO();
        BizResult bizResult = elecRuleBiz.update(elecRuleDO);
        if (bizResult.success) {
            return "redirect:/fee/elec/list.htm";
        } else {
            return "redirect:/error.htm?code="+bizResult.code;
        }

    }

    @RequestMapping(value = "fee/elec/doDelete.htm")
    public void doDelete(HttpServletRequest request, HttpServletResponse response,ModelMap modelMap) {
        long id = RequestUtil.getLong(request, "id");
        BizResult bizResult = elecRuleBiz.delete(id);
        JsonResult result = JsonResult.getInstance();
        if (bizResult.success) {
            ResponseHelper.ajaxReturn(result.getMap(), response);
            return;
        } else {
            result.setErrMsg(ErrorCode.getMessage(bizResult.code));
            ResponseHelper.ajaxReturn(result.getMap(), response);
            return;
        }

    }
}
